﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.Entity;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.SystemManagement
{
    public partial class changePassword : BasePage
    {
        private IUserBLL _userBll = IocContainer.Instance.Resolve<IUserBLL>();
        private Skyworth.CRM.Service.Entity.Users user;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                    int userId = 0;
                    int.TryParse(Request.QueryString["userId"], out userId);
                    if (userId > 0)
                    {
                        user = _userBll.GetModel(userId);
                        this.txtUserName.Text = user.UserName;
                        ViewState["user"] = user;
                    }
            }
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                user = (Users)ViewState["user"];
                string oldPW = Encrypt.MD5(this.txtOldPw.Text.Trim(), 32);
                string newPw = Encrypt.MD5(this.txtNewPW.Text.Trim(), 32);
                if (String.Compare(user.Password, oldPW, true) == 0)
                {
                    user.Password = newPw;
                    //user.LastModifyTime = DateTime.Now;
                    _userBll.Update(user);

                    ClientScript.RegisterClientScriptBlock(this.GetType(), "closeParentPage", "<script language=\"JavaScript\">parent.addWindow.close();alert('修改密码成功,下次登录生效！');</script>");
                   
                }
                else
                {
                    Alert("请输入正确的旧密码！");
                }
            }
            catch (Exception ex)
            {

                Alert("发生异常：\r\n"+ex.Message);
            }
           
        }
    }
}
